package com.yegs.jltqc.U8ServiceImpl;

import com.yegs.jltqc.TO.NamePageQuery;
import com.yegs.jltqc.TO.U8SelectMomorderQuery;
import com.yegs.jltqc.U8Service.U8SelectMomorderService;
import com.yegs.jltqc.U8Service.U8VendorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 质检>>收料
 */
@Service
public class U8SelectMomorderServiceImpl implements U8SelectMomorderService {

    @Autowired
    @Qualifier("u8JdbcTemplate")
    private JdbcTemplate u8JdbcTemplate;

    @Override
    public List<Map<String,Object>> query(String mocode,String socode) {
        List<Map<String,Object>> list = null;
        String sql = "select top 100 * from JLTSystem.dbo.ymh_SelectMomorder ";
        if(mocode != null && socode != null){
            sql +="where mocode like ? and socode like ? order by modid desc";
            list = u8JdbcTemplate.queryForList(sql,"%"+mocode+"%","%"+socode+"%");
        }else if(mocode != null){
            sql +=" where mocode like ? order by modid desc";
            list = u8JdbcTemplate.queryForList(sql,"%"+mocode+"%");
        }else if(socode != null){
            sql +=" where socode like ? order by modid desc";
            list = u8JdbcTemplate.queryForList(sql,"%"+socode+"%");
        }else{
            sql +=" order by modid desc";
            list = u8JdbcTemplate.queryForList(sql);
        }
        return list;
    }

    public Page<Map<String, Object>> queryByPage(U8SelectMomorderQuery query) {
        String mocode = query.getMocode();
        String socode = query.getSocode();
        List<Map<String,Object>> list = null;
        String sql = "SELECT TOP " + (query.getPageIndex() * query.getPageSize())+
                " ROW_NUMBER() OVER(ORDER BY modid desc) AS ROWID, * FROM JLTSystem.dbo.ymh_SelectMomorder ";
        if(mocode != null && socode != null){
            sql +="where mocode like ? and socode like ? order by modid desc";
            sql = "select * from ("+sql +") as temp1 where ROWID > " + (query.getPageIndex()-1) * query.getPageSize();
            list = u8JdbcTemplate.queryForList(sql,"%"+mocode+"%","%"+socode+"%");
        }else if(mocode != null){
            sql +=" where mocode like ? order by modid desc";
            sql = "select * from ("+sql +") as temp1 where ROWID > " + (query.getPageIndex()-1) * query.getPageSize();
            list = u8JdbcTemplate.queryForList(sql,"%"+mocode+"%");
        }else if(socode != null){
            sql +=" where socode like ? order by modid desc";
            sql = "select * from ("+sql +") as temp1 where ROWID > " + (query.getPageIndex()-1) * query.getPageSize();
            list = u8JdbcTemplate.queryForList(sql,"%"+socode+"%");
        }else{
            sql +=" order by modid desc";
            sql = "select * from ("+sql +") as temp1 where ROWID > " + (query.getPageIndex()-1) * query.getPageSize();
            list = u8JdbcTemplate.queryForList(sql);
        }
        sql = "select count (*) from JLTSystem.dbo.ymh_SelectMomorder ";

        if(mocode != null && socode != null){
            sql +="where mocode like '%"+mocode+"%' and socode like '%"+socode+"%'";
        }else if(mocode != null){
            sql +=" where mocode like '%"+mocode+"%'";
        }else if(socode != null){
            sql +=" where socode like '%"+socode+"%'";
        }
        int total = u8JdbcTemplate.queryForObject(sql,Integer.class);
        PageRequest request = PageRequest.of(query.getPageIndex() -1,query.getPageSize());
        PageImpl page = new PageImpl(list,request,total);
        return page;
    }
}
